<?php
namespace app\admin\controller;

use app\admin\model\Menus;
use app\admin\model\Campus;
use app\admin\model\Student;
use app\admin\model\Classes;
use app\admin\model\Teacher;
use think\Controller;
use think\Db;


/**
 * 后台主页
 * Class IndexController
 * Author ghostsf
 * Blog www.ghostsf.com
 * @package app\admin\controller
 */
class IndexController extends CommonController
{
    /**
     *
     * @return mixed
     */
    public function index()
    {
        $userSession = session('loginSession');
        //=====================================
        if($userSession['from'] == 'super'){
            session("loginSession", null);
            return $this->fetch('index');
        }
        //=====================================
        if (!$userSession) {
            $this->setGlobalSettings(["GlobalTitle", "LoginPageTitle", "DialogClassSpeed", "DialogClassAnimation", "LoginPageSTitle"]);
            return $this->fetch('index');
        } else {
            //先去选择团队
            if(empty($_POST['campusId'])){
                $uid = $userSession['user_id'];
                $teamids = Db::table('up_team_members')
                    ->where('team_member_user_id',$uid)
                    ->where('team_member_role','<',4)
                    ->where('team_member_remove',2)
                    ->field('team_id')
                    ->select();
//                var_dump($teamids);die;
                $ids = "";
                $campus = array();
                if($teamids){
                    foreach ($teamids as $v){
                        $ids .= $v['team_id'] . ',';
                    }
                    $ids = trim($ids,',');
                    $campus = Db::table('up_team')
                        ->where('team_id','in',$ids)
                        ->where('team_status',2)
                        ->where('status',1)
                        ->field('team_id,team_number,team_name,team_time')
                        ->select();
                }
                $this->assign("campus", $campus);
                return $this->fetch("campus");
            }
            //============
            //选完保存选择信息
            if(!empty($_POST['campusId'])){
                $teamid = $_POST['campusId'];
                    $team = Db::table('up_team')
                        ->where('team_id',$teamid)
                        ->field('team_id,team_number,team_name,team_time')
                        ->find();
//                var_dump($team);die;
                $userSession['team_id'] = $team['team_id'];
                $userSession['team_number'] = $team['team_number'];
                $userSession['team_name'] = $team['team_name'];
                session('loginSession',$userSession);
            }
            //=======end=====

            $this->setGlobalSettings(["DialogClassSpeed", "DialogClassAnimation", "GlobalTitle", "BackGroundTitle"]);
//            $this->setPersonalSettings(["openAnimation"]);
            $menus = Menus::all();
            $menunames = array();
            $menuicons = array();
            foreach ($menus as $menu) {
                $menunames[$menu['menu_value']] = $menu['menu_name'];
                $menuicons[$menu['menu_value']] = $menu['menu_iconclass'];
            }

            $this->assign("menunames", $menunames);
            $this->assign("menuicons", $menuicons);
            $this->assign("user", $userSession);
//            print_r($menunames);
//            print_r($menuicons);die;
            $this->assign('team_name',$team['team_name']);
            return $this->fetch('admin');
        }
    }

    /**
     * 查找队伍
     */
    public function getSearchTeam()
    {
        $userSession = session('loginSession');
        $uid = $userSession['user_id'];
        $teamids = Db::table('up_team_members')
            ->where('team_member_user_id',$uid)
            ->where('team_member_role','<',4)
            ->where('team_member_remove',2)
            ->field('team_id')
            ->select();
//                var_dump($teamids);die;
        $ids = "";
        $campus = array();
        if($teamids){
            foreach ($teamids as $v){
                $ids .= $v['team_id'] . ',';
            }
            $ids = trim($ids,',');
            $where = array();
            $where['status'] = 1;
            if(isset($_POST['search']) && !empty($_POST['search'])){
                $search = trim($_POST['search'],' ');
                $where['team_name'] = array('like','%'.$search.'%');
            }
            $campus = Db::table('up_team')
                ->where('team_id','in',$ids)
                ->where('team_status',2)
                ->where($where)
                ->field('team_id,team_number,team_name,team_time')
                ->select();
        }
        if(empty($campus)){
            return 1;
        }else{
            $this->assign("campus", $campus);
            return $this->fetch("ajax_team");
        }
    }
    


}
